
	--*********************************************************************
--*  Returns the number of materials associated with the specified mesh
--*  
fn getNumMaterials pMesh =
(
	if pMesh == undefined then
		return 0
	
	local numFaces	= pMesh.numfaces
	local materials = #()
	
	for i = 1 to numFaces do
	(
		faceMaterial = getFaceMatID pMesh i
		if ((findItem materials faceMaterial) == 0) then
			append materials faceMaterial
	)
	local materialCount = materials.count
	return materialCount
)

--*********************************************************************
--*  Returns groups of geometry based on the material composition of the
--*  specified mesh
--* 
fn buildPartCollections pMesh pMaterial =
(
	if pMesh == undefined or pMaterial == undefined then
		return #()
	
	local numFaces	= pMesh.numfaces
	local numParts = getNumMaterials pMesh
	if numParts == 0 then
		return #()
	
	local numSubmaterials = 0
	if pMaterial != undefined then
		numSubmaterials = getNumSubMtls pMaterial	
	
	lastMaterial = 0
	lastMaterialIdx = 0
	collections = #()
	
	for m = 1 to numSubmaterials do
	(
		pCollectionMaterial = getSubMtl pMaterial m
		
		for i = 1 to numFaces do
		(
			faceMaterialIdx = getFaceMatID pMesh i
			if faceMaterialIdx == m then
			(
			--	print ("Found match")
			--	print ("Mat: " + (m as string) + " Facemat: " + (faceMaterialIdx as string) + " Face: " + (i as string))
			--	print ("Last Mat: " + (lastMaterial as string))
				if (lastMaterial != m) then
				(
					append collections #(pCollectionMaterial, #())
					lastMaterial = m
					lastMaterialIdx += 1
				)
				append collections[lastMaterialIdx][2] i
			)
		)
	)
	--print ("number parts:" + (lastMaterial as string))	
	--print ("number inital parts:" + (numParts as string))	
		
	return collections
)

--*********************************************************************
--*  Untangles the mess/relation between vertices/texture coordinates
--*  
fn getTextureCoordinates pMesh faceIndex mapChannel = 
(
	if pMesh ==undefined then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	if faceIndex < 1 or faceIndex > pMesh.numfaces then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	
	local face = getFace pMesh faceIndex
	local numTexs	= pMesh.numtverts
	local texCoords	= #()
	
	mapFace = meshop.getMapFace pMesh mapChannel faceIndex
	
	for i = 1 to 3 do
	(
		uvwCoord = meshop.getMapVert pMesh mapChannel mapFace[i]
		texCoords[i] = point2 uvwCoord.x -uvwCoord.y
	)
	
	return texCoords
)

--*********************************************************************
--*  Gets the normals for vertices of specified face
--*  
fn getNormals pMesh faceIndex = 
(
	if pMesh ==undefined then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	if faceIndex < 1 or faceIndex > pMesh.numfaces then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	
	local face = getFace pMesh faceIndex
	local normals	= #()
	
	for i = 1 to 3 do
	(
		normals[i] = normalize ( getFaceNormal pMesh faceIndex)
		--normals[i] = normalize ( getNormal pMesh face[i] )
		--print ("Face: " + (faceIndex as string) + "Vtx: " + (face[i] as string) + " Nrm: " + (normals[i] as string))
	)
	
	
	
	return normals
)

--*********************************************************************
--*  Gets positions of vertices for specified face
--*  
fn getPositions pMesh faceIndex = 
(
	if pMesh ==undefined then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	if faceIndex < 1 or faceIndex > pMesh.numfaces then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	
	
	local face = getFace pMesh faceIndex
	local positions	= #()
	
	for i = 1 to 3 do
	(
		positions[i] = getVert pMesh face[i]
		--print ("x: " + (positions[i].x as string))
		--print ("y: " + (positions[i].y as string))
	--	print ("z: " + (positions[i].z as string))
	)
	
	return positions
)

--*********************************************************************
--*  Gets tangents of vertices for specified face
--*  
fn getTangents pMesh faceIndex = 
(
	if pMesh ==undefined then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	if faceIndex < 1 or faceIndex > pMesh.numfaces then
		return #(point3 0 0 0, point3 0 0 0, point3 0 0 0)
	
	local positions = getPositions pMesh faceIndex	
	local texCoords = getTextureCoordinates pMesh faceIndex 1
	
	local vA = positions[2] - positions[1]
	local vB = positions[3] - positions[1]
	
	local dU1 = texCoords[2][1] - texCoords[1][1]
	local dU2 = texCoords[3][1] - texCoords[1][1]
	local dV1 = texCoords[2][2] - texCoords[1][2]
	local dV2 = texCoords[3][2] - texCoords[1][2]
	
	local vU = point3 0 0 0
	local vV = point3 0 0 0
	local vN = point3 0 0 0
	
	local div = ((dU1 * dV2) - (dU2 * dV1))
	
	if div != 0.0 then
	(
		local area = abs ( div )
	
		local a = dV2 / div
		local b = -dV1 / div
		local c = -dU2 / div
		local d = dU1 / div
		
		vU = normalize ( (vA * a + vB * b) * area )
		vV = normalize ( (vA * c + vB * d) * area )
		vN = normalize ( cross vA vB )
	)		
	return #(vV, vV, vV)
)


--*********************************************************************
--*  Exports the specified mesh to the engines file format
--*  
fn exportMeshPartData pMesh pMaterial triangleIndicies = 
(
	if pMesh == undefined or pMaterial == undefined or triangleIndicies == undefined then
		return
		
	local indiciesCount = undefined
	indiciesCount = triangleIndicies.count
	writelong gFilestream indiciesCount
	
	if indiciesCount > 0 then
	(
		exportMaterial pMaterial
		for i = 1 to indiciesCount do
		(
			triangleIdx = triangleIndicies[i]
			faceTextureCoordinates = (getTextureCoordinates pMesh triangleIdx 1)
			
			faceGICoordinates = undefined
			if ((gExportBakedCoordinates == true) and (meshop.getMapSupport pMesh 2)) then 
				faceGICoordinates = (getTextureCoordinates pMesh triangleIdx 2)
			
			faceVertexNormals = (getNormals pMesh triangleIdx)
			faceVertexPositions = (getPositions pMesh triangleIdx)
			faceVertexTangents = (getTangents pMesh triangleIdx)

			
			for k = 1 to 3 do
			(
				local j = triOrder[k]
				WriteVector faceVertexPositions[j] 
				WriteVector faceVertexNormals[j]
				WriteVector faceVertexTangents[j]
				WriteTexCoord  faceTextureCoordinates[j]
				if ((gExportBakedCoordinates == true) and (meshop.getMapSupport pMesh 2)) then 
				(
					WriteTexCoord  faceGICoordinates[j]
				)
				else
				(
					WriteTexCoord faceTextureCoordinates[j]
				)
			)
		)
	)
	else
	(
		--print "       no part data written"
	)
)
